package spring.httpClient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class RestFulCommmon {
	public static String common (String appid,String token){
		CloseableHttpClient httpclient=HttpClients.createDefault();
		String RESTFUL_BASE_URL="http://127.0.0.1:8080/creditManagementSystem-web/company/list";
		HttpPost httppost=new HttpPost(RESTFUL_BASE_URL);
		// 创建参数队列  
		List<NameValuePair> formparams=new ArrayList<NameValuePair>();
		//传Integer类型的直接用引号
		formparams.add(new BasicNameValuePair("start", "0"));
		formparams.add(new BasicNameValuePair("length", "5"));

		UrlEncodedFormEntity uefEntity = null;
		 try {  
	            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
	            httppost.setEntity(uefEntity);  
	            CloseableHttpResponse response = httpclient.execute(httppost);  
	            try {  
	                HttpEntity entity = response.getEntity();  
	                if (entity != null) {  
	                    String jsonStr = EntityUtils.toString(entity, "UTF-8");
	                    return jsonStr;
	                }  
	            } finally {  
	                response.close();  
	            }  
	        } catch (ClientProtocolException e) {  
	            e.printStackTrace();  
	        } catch (UnsupportedEncodingException e1) {  
	            e1.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }catch(Exception e){
	        	e.printStackTrace();
	        } finally {  
	            // 关闭连接,释放资源    
	            try {  
	                httpclient.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	        return null;
	  }
	  
	public static void main(String[] args) {
		String a=RestFulCommmon.common("appid", "token");
		System.out.println(a);
	    }	  
	
}
